package de.hfu.android;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.widget.EditText;
import android.widget.RatingBar;
import android.widget.RatingBar.OnRatingBarChangeListener;
import android.widget.TextView;
import de.hfu.helper.DataAccess;
import de.hfu.helper.DataCarrier;
import de.hfu.helper.DataParser;
import de.hfu.helper.Helper;
import de.hfu.model.Dish;

public class MenuViewActivity extends Activity implements
		OnRatingBarChangeListener {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.menuview);

		TextView lblMenuName = (TextView) findViewById(R.id.lblMenuName);
		TextView lblDish = (TextView) findViewById(R.id.lblDish);
		RatingBar ratebar = (RatingBar) findViewById(R.id.ratebar);

		Dish curDish = DataCarrier.getInstance().getDish();
		String dishLanguage = DataAccess.getInstance(this).getDishLanguage();

		lblMenuName.setText(curDish.getTitle(dishLanguage));
		lblDish.setText(curDish.getDescription(dishLanguage));

		ratebar.setRating((float) curDish.getRating());

		ratebar.setOnRatingBarChangeListener(this);

		EditText comment = (EditText) findViewById(R.id.txtComment);
		comment.setText(curDish.getComment());
				
		if (DataCarrier.getInstance().getDishKennzeichnung() != null) {
			TextView lblDesc = (TextView) findViewById(R.id.lblDesc);
			lblDesc.setText(DataCarrier.getInstance().getDishKennzeichnung().getDescription(dishLanguage));
		}
	}

	@Override
	public void onRatingChanged(RatingBar ratingBar, float rating,
			boolean fromUser) {
		Dish curDish = DataCarrier.getInstance().getDish();
		
		if (!Helper.isEqual(rating, curDish.getRating())) {
			SharedPreferences prefs = PreferenceManager
					.getDefaultSharedPreferences(this);
			SharedPreferences.Editor editor = prefs.edit();
			editor.putBoolean("changedLocal", true);
			editor.commit();
			
			curDish.setRating(rating);
			DataCarrier.getInstance().setChanged(true);
			curDish.setPostedRating(false);
		}
	}

	private void getNewComment() {
		EditText txtComment = (EditText) findViewById(R.id.txtComment);
		String comment = txtComment.getText().toString().trim();
		Dish curDish = DataCarrier.getInstance().getDish();
		if (!curDish.getComment().equals(comment)) {
			curDish.setComment(comment);
			if (comment.length() > 0) {
				DataCarrier.getInstance().setChanged(true);
				curDish.setPostedComment(false);
			}
		}
	}

	@Override
	protected void onPause() {

		getNewComment();

		DataCarrier carrier = DataCarrier.getInstance();
		DataAccess dac = DataAccess.getInstance(this);
		
		Dish dish = carrier.getDish();

		if (carrier.isChanged()) {						
			dac.updateDish(dish);
			carrier.setChanged(false);						
		}
		
		new PostCommentAndRatingTask(this).execute();

		super.onPause();
	}

	@Override
	protected void onStart() {
		new GetCommentTask(this).execute();

		super.onStart();
	}
}
